<template>
  <el-container class="layout-container">
    <el-header class="header">
      <div class="header-left">
        <div class="logo">
          <h3>大学生自选宿舍系统</h3>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="top-menu"
          router
          mode="horizontal"
          background-color="#74b9ff"
          text-color="rgba(255,255,255,0.8)"
          active-text-color="#ffffff"
        >
          <el-menu-item index="/student/dashboard">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          
          <el-menu-item index="/student/announcements">
            <el-icon><Bell /></el-icon>
            <span>校园公告</span>
          </el-menu-item>
          
          <el-menu-item index="/student/select-room">
            <el-icon><OfficeBuilding /></el-icon>
            <span>在线选宿</span>
          </el-menu-item>
          
          <el-menu-item index="/student/profile">
            <el-icon><User /></el-icon>
            <span>个人中心</span>
          </el-menu-item>
          
          <el-menu-item index="/student/payment">
            <el-icon><CreditCard /></el-icon>
            <span>费用支付</span>
          </el-menu-item>
          
          <el-menu-item index="/student/questionnaire">
            <el-icon><Document /></el-icon>
            <span>问卷调查</span>
          </el-menu-item>
          
          <el-menu-item index="/student/repair">
            <el-icon><Tools /></el-icon>
            <span>宿舍报修</span>
          </el-menu-item>
        </el-menu>
      </div>
      
      <div class="header-right">
        <el-dropdown @command="handleCommand">
          <span class="user-info">
            <el-icon><Avatar /></el-icon>
            {{ userStore.userInfo.username }}
            <el-icon class="el-icon--right"><ArrowDown /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">个人信息</el-dropdown-item>
              <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    
    <el-container>
      <el-main class="main-content">
        <div class="content-header">
          <h2 class="page-title">{{ currentPageTitle }}</h2>
        </div>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useUserStore } from '../stores/user'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const activeMenu = computed(() => route.path)

const currentPageTitle = computed(() => {
  const titleMap = {
    '/student/dashboard': '首页',
    '/student/announcements': '查看公告',
    '/student/select-room': '选择宿舍',
    '/student/profile': '个人中心',
    '/student/payment': '费用支付',
    '/student/questionnaire': '问卷调查',
    '/student/repair': '宿舍报修'
  }
  return titleMap[route.path] || '学生系统'
})

const handleCommand = async (command) => {
  if (command === 'profile') {
    router.push('/student/profile')
  } else if (command === 'logout') {
    try {
      await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      
      await userStore.logout()
      ElMessage.success('退出登录成功')
      router.push('/login')
    } catch (error) {
      // 用户取消操作
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.header {
  background: linear-gradient(135deg, #74b9ff 0%, #0984e3 100%);
  border: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-left {
  display: flex;
  align-items: center;
  flex: 1;
}

.logo {
  margin-right: 40px;
}

.logo h3 {
  margin: 0;
  font-size: 18px;
  color: white;
  font-weight: 500;
}

.top-menu {
  border: none;
  background: transparent;
  flex: 1;
}

.top-menu :deep(.el-menu-item) {
  border-bottom: 2px solid transparent;
  color: rgba(255,255,255,0.8);
  font-weight: 400;
  margin: 0 5px;
  padding: 0 15px;
}

.top-menu :deep(.el-menu-item:hover) {
  background-color: rgba(255,255,255,0.1);
  color: white;
}

.top-menu :deep(.el-menu-item.is-active) {
  border-bottom-color: white;
  background-color: rgba(255,255,255,0.15);
  color: white;
}

.top-menu :deep(.el-menu-item .el-icon) {
  margin-right: 5px;
}

.header-right .user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: white;
  padding: 8px 16px;
  border-radius: 20px;
  transition: all 0.3s;
}

.header-right .user-info:hover {
  background-color: rgba(255,255,255,0.1);
}

.header-right .user-info .el-icon {
  margin: 0 4px;
}

.main-content {
  background-color: #f5f7fa;
  padding: 0;
  overflow-y: auto;
}

.content-header {
  background: white;
  padding: 20px 30px;
  border-bottom: 1px solid #e4e7ed;
  margin-bottom: 0;
}

.page-title {
  margin: 0;
  font-size: 20px;
  font-weight: 500;
  color: #303133;
}

.main-content :deep(.el-card) {
  margin: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}
</style> 